<template>
  <div class="app-container">
    <!-- <div class="filter-container" style="border: 1px solid #dcdcdc;padding: 20px;margin-bottom: 20px;">
      <el-link :type="listQuery.audit_status == undefined ? 'primary' : ''" :underline="false" @click="tabChange(undefined)">全部</el-link>
      <el-link :type="listQuery.audit_status == 0 ? 'primary' : ''" :underline="false" style="margin-left:60px;" @click="tabChange(0)">待审核</el-link>
      <el-link :type="listQuery.audit_status == 1 ? 'primary' : ''" :underline="false" style="margin-left:60px;" @click="tabChange(1)">审核通过</el-link>
      <el-link :type="listQuery.audit_status == 2 ? 'primary' : ''" :underline="false" style="margin-left:60px;" @click="tabChange(2)">拒绝</el-link>
    </div> -->
    <!-- 表格 -->
    <el-table
      :key="tableKey"
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="申请会员" prop="user.name" align="center" />
      <el-table-column label="店铺名称" prop="shopping_name" align="center">
        <template slot-scope="{ row }">
          {{ row.name || row.shopping_name }}
        </template>
      </el-table-column>
      <el-table-column label="企业名称" prop="company_name" align="center" />
      <el-table-column label="所在地" align="center">
        <template
          slot-scope="{ row }"
        >{{
          row.company_province && row.company_province.length > 0
            ? row.company_province[0].title
            : ""
        }}{{
          row.company_city && row.company_city.length > 0
            ? row.company_city[0].title
            : ""
        }}{{
          row.company_country && row.company_country.length > 0
            ? row.company_country[0].title
            : ""
        }}{{ row.company_area }}</template>
      </el-table-column>
      <el-table-column label="保证金" align="center" prop="security_deposit" />
      <el-table-column align="center" label="申请时间" prop="created_at" />
      <el-table-column align="center" label="审核状态">
        <template slot-scope="{ row }">
          {{
            row.audit_status == 0
              ? "待审核"
              : row.audit_status == 1
                ? "审核通过"
                : row.audit_status == 2
                  ? "不通过"
                  : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="{ row }">
          <el-button
            v-if="row.audit_status == 0"
            size="mini"
            plain
            type="success"
            @click="openHandleAgree(row.id, row.shopping_name, row)"
          >
            审核通过
          </el-button>
          <el-button
            v-if="row.audit_status == 0"
            size="mini"
            plain
            type="danger"
            @click="handleRefuse(row.id, row.shopping_name)"
          >
            拒绝
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div v-if="total > 0" style="text-align: right">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.per_page"
        @pagination="getList"
      />
    </div>

    <el-dialog
      title="审核"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="40%"
    >
      <el-form ref="postForm1" :model="form1" class="form-container">
        <el-form-item label-width="90px" label="法人:">
          {{ form1.company_user }}
        </el-form-item>
        <el-form-item label-width="90px" label="法人信息:">
          <img
            style="
              width: 148px;
              height: 148px;
              display: inline-block;
              margin-right: 8px;
              margin-bottom: 8px;
            "
            :src="form1.company_user_header"
          >
          <img
            style="
              width: 148px;
              height: 148px;
              display: inline-block;
              margin-right: 8px;
              margin-bottom: 8px;
            "
            :src="form1.company_user_back"
          >
        </el-form-item>
        <el-form-item label-width="90px" label="店铺名称:">
          {{ form1.name || form1.shopping_name }}
        </el-form-item>
        <el-form-item label-width="90px" label="店铺描述:">
          {{ form1.shopping_remark }}
        </el-form-item>
        <el-form-item label-width="90px" label="企业名称:">
          {{ form1.company_name }}
        </el-form-item>
        <el-form-item label-width="90px" label="所在地:">
          {{
            form1.company_province && form1.company_province.length > 0
              ? form1.company_province[0].title
              : ""
          }}{{
            form1.company_city && form1.company_city.length > 0
              ? form1.company_city[0].title
              : ""
          }}{{
            form1.company_country && form1.company_country.length > 0
              ? form1.company_country[0].title
              : ""
          }}{{ form1.company_area }}
        </el-form-item>
        <el-form-item label-width="90px" label="保证金:">
          {{ form1.security_deposit }}
        </el-form-item>
        <el-form-item label-width="90px" label="申请时间:">
          {{ form1.created_at }}
        </el-form-item>
        <el-form-item label="店铺资料:" prop="image">
          <img
            style="
              width: 148px;
              height: 148px;
              display: inline-block;
              margin-right: 8px;
              margin-bottom: 8px;
            "
            :src="form1.logo"
          >
          <img
            style="
              width: 148px;
              height: 148px;
              display: inline-block;
              margin-right: 8px;
              margin-bottom: 8px;
            "
            :src="form1.business_image"
          >
        </el-form-item>
      </el-form>
      <div style="margin-top: 40px; text-align: center">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button
          type="danger"
          @click="handleAgree(form1.id, form1.shopping_name)"
        >审核通过</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getShopping, updateShopping } from '@/api/common'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'MerchantsRequest',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  data() {
    return {
      form1: {},
      dialogVisible: false,
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      form: {},
      listQuery: {
        page: 1,
        per_page: 20,
        type: 1,
        audit_status: 0
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    tabChange(e) {
      this.listQuery.audit_status = e
      this.handleFilter()
    },
    getList() {
      this.listLoading = true
      getShopping(this.listQuery)
        .then((response) => {
          const { data, total } = response.data
          this.list = data
          this.total = total
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    // 拒绝
    handleRefuse(id) {
      const that = this
      this.$prompt('', '拒绝', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /\S/,
        inputPlaceholder: '请输入拒绝原因',
        inputErrorMessage: '拒绝原因不能为空',
        inputType: 'textarea',
        closeOnPressEscape: false,
        closeOnClickModal: false,
        closeOnHashChange: false,
        callback() {},
        beforeClose(action, instance, done) {
          if (action === 'confirm') {
            if (instance.confirmButtonLoading) {
              return false
            }
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '操作中...'
            updateShopping(id, {
              audit_status: 2,
              reject_remark: instance.inputValue
            })
              .then(() => {
                that.getList()
                that.$notify({
                  title: '提示',
                  message: '操作成功',
                  type: 'success',
                  duration: 2000
                })
                done()
              })
              .finally(() => {
                instance.confirmButtonLoading = false
                instance.confirmButtonText = '确定'
              })
          } else {
            if (!instance.confirmButtonLoading) {
              done()
            } else {
              that.$message.warning('操作中，禁止关闭')
            }
          }
        }
      })
    },
    openHandleAgree(id, name, row) {
      this.form1 = row
      this.dialogVisible = true
      console.log(row)
      return
    },
    handleAgree(id, name) {
      const that = this
      this.$confirm(`请确认是否通过${name}的商户入驻申请？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          updateShopping(id, {
            audit_status: 1
          }).then(() => {
            this.dialogVisible = false
            that.getList()
            that.$notify({
              title: '提示',
              message: '操作成功',
              type: 'success',
              duration: 2000
            })
          })
        })
        .catch(() => {})
    },
    // 查看
    handleLook(row) {
      this.$router.push('/member/detail/' + row.id)
    }
  }
}
</script>
